package com.apikey.auth.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import java.time.LocalDateTime;

/**
 * API Key历史记录实体类
 * 记录API Key的变更历史
 *
 * @author API Key Framework
 * @version 1.0
 */
@Entity
@Table(name = "api_key_history", indexes = {
        @Index(name = "idx_key_value", columnList = "key_value"),
        @Index(name = "idx_user_id", columnList = "user_id"),
        @Index(name = "idx_operator_id", columnList = "operator_id"),
        @Index(name = "idx_operation_type", columnList = "operation_type"),
        @Index(name = "idx_updated_at", columnList = "updated_at"),
        @Index(name = "idx_is_deleted", columnList = "is_deleted")
})
public class History {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "key_value", nullable = false, length = 64)
    private String keyValue;

    @Column(name = "name", nullable = false, length = 100)
    private String name;

    @Column(name = "permissions", columnDefinition = "TEXT")
    private String permissions;

    @Column(name = "status", nullable = false, length = 20)
    private String status;

    @Column(name = "daily_limit")
    private Integer dailyLimit;

    @Column(name = "monthly_limit")
    private Integer monthlyLimit;

    @Column(name = "expires_at")
    private LocalDateTime expiresAt;

    @Column(name = "user_id", length = 50)
    private String userId;

    @Column(name = "operator_id", length = 50)
    private String operatorId;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Column(name = "operation_type", nullable = false, length = 20)
    private String operationType;

    @Column(name = "change_reason", columnDefinition = "TEXT")
    private String changeReason;

    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;

    /**
     * 默认构造函数
     */
    public History() {
        this.updatedAt = LocalDateTime.now();
        this.isDeleted = false;
    }

    /**
     * 从API Key创建历史记录的构造函数
     *
     * @param apiKey        API Key实体
     * @param operationType 操作类型
     * @param operatorId    操作者ID
     */
    public History(ApiKey apiKey, String operationType, String operatorId) {
        this();
        this.keyValue = apiKey.getKeyValue();
        this.name = apiKey.getName();
        this.permissions = apiKey.getPermissions();
        this.status = apiKey.getStatus();
        this.dailyLimit = apiKey.getDailyLimit();
        this.monthlyLimit = apiKey.getMonthlyLimit();
        this.expiresAt = apiKey.getExpiresAt();
        this.userId = apiKey.getUserId();
        this.operatorId = operatorId;
        this.description = apiKey.getDescription();
        this.operationType = operationType;
    }

    // Getter和Setter方法

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getKeyValue() {
        return keyValue;
    }

    public void setKeyValue(String keyValue) {
        this.keyValue = keyValue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPermissions() {
        return permissions;
    }

    public void setPermissions(String permissions) {
        this.permissions = permissions;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Integer getDailyLimit() {
        return dailyLimit;
    }

    public void setDailyLimit(Integer dailyLimit) {
        this.dailyLimit = dailyLimit;
    }

    public Integer getMonthlyLimit() {
        return monthlyLimit;
    }

    public void setMonthlyLimit(Integer monthlyLimit) {
        this.monthlyLimit = monthlyLimit;
    }

    public LocalDateTime getExpiresAt() {
        return expiresAt;
    }

    public void setExpiresAt(LocalDateTime expiresAt) {
        this.expiresAt = expiresAt;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getOperatorId() {
        return operatorId;
    }

    public void setOperatorId(String operatorId) {
        this.operatorId = operatorId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getOperationType() {
        return operationType;
    }

    public void setOperationType(String operationType) {
        this.operationType = operationType;
    }

    public String getChangeReason() {
        return changeReason;
    }

    public void setChangeReason(String changeReason) {
        this.changeReason = changeReason;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Boolean getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(Boolean isDeleted) {
        this.isDeleted = isDeleted;
    }

    /**
     * 获取操作类型的中文描述
     *
     * @return 操作类型描述
     */
    public String getOperationTypeLabel() {
        switch (operationType) {
            case "CREATE":
                return "创建";
            case "UPDATE":
                return "更新";
            case "DELETE":
                return "删除";
            case "ACTIVATE":
                return "激活";
            case "DEACTIVATE":
                return "停用";
            case "RENEW":
                return "续期";
            case "RESTORE":
                return "恢复";
            default:
                return operationType;
        }
    }

    /**
     * 检查是否为创建操作
     *
     * @return 是否为创建操作
     */
    public boolean isCreateOperation() {
        return "CREATE".equals(operationType);
    }

    /**
     * 检查是否为更新操作
     *
     * @return 是否为更新操作
     */
    public boolean isUpdateOperation() {
        return "UPDATE".equals(operationType);
    }

    /**
     * 检查是否为删除操作
     *
     * @return 是否为删除操作
     */
    public boolean isDeleteOperation() {
        return "DELETE".equals(operationType);
    }

    /**
     * 检查是否为状态变更操作
     *
     * @return 是否为状态变更操作
     */
    public boolean isStatusChangeOperation() {
        return "ACTIVATE".equals(operationType) || "DEACTIVATE".equals(operationType);
    }

    /**
     * 标记为已删除
     */
    public void markAsDeleted() {
        this.isDeleted = true;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 恢复记录
     */
    public void restore() {
        this.isDeleted = false;
        this.updatedAt = LocalDateTime.now();
    }

    @Override
    public String toString() {
        return "History{" +
                "id=" + id +
                ", keyValue='" + keyValue + '\'' +
                ", name='" + name + '\'' +
                ", status='" + status + '\'' +
                ", operationType='" + operationType + '\'' +
                ", operatorId='" + operatorId + '\'' +
                ", updatedAt=" + updatedAt +
                ", isDeleted=" + isDeleted +
                '}';
    }
}